﻿using System;
using System.Collections.Generic;
using System.Text;
using Hime.Redist.Parsers;

namespace xowl.gmi.compiler.transform
{
    class PatternOWL : Pattern
    {
        private string axiom;
        private List<PatternPart> parameters;
        private bool isMeta;
        private bool isNegative;

        public PatternOWL(string name, PatternPart p1)
        {
            this.axiom = name;
            this.parameters = new List<PatternPart>();
            this.parameters.Add(p1);
        }
        public PatternOWL(string name, PatternPart p1, bool isMeta, bool isNegative)
        {
            this.axiom = name;
            this.parameters = new List<PatternPart>();
            this.parameters.Add(p1);
            this.isMeta = isMeta;
            this.isNegative = isNegative;
        }
        public PatternOWL(string name, PatternPart p1, PatternPart p2)
        {
            this.axiom = name;
            this.parameters = new List<PatternPart>();
            this.parameters.Add(p1);
            this.parameters.Add(p2);
        }
        public PatternOWL(string name, PatternPart p1, PatternPart p2, bool isMeta, bool isNegative)
        {
            this.axiom = name;
            this.parameters = new List<PatternPart>();
            this.parameters.Add(p1);
            this.parameters.Add(p2);
            this.isMeta = isMeta;
            this.isNegative = isNegative;
        }
        public PatternOWL(string name, PatternPart p1, PatternPart p2, PatternPart p3)
        {
            this.axiom = name;
            this.parameters = new List<PatternPart>();
            this.parameters.Add(p1);
            this.parameters.Add(p2);
            this.parameters.Add(p3);
        }
        public PatternOWL(string name, PatternPart p1, PatternPart p2, PatternPart p3, bool isMeta, bool isNegative)
        {
            this.axiom = name;
            this.parameters = new List<PatternPart>();
            this.parameters.Add(p1);
            this.parameters.Add(p2);
            this.parameters.Add(p3);
            this.isMeta = isMeta;
            this.isNegative = isNegative;
        }
        public PatternOWL(Context context, SyntaxTreeNode ast, bool isMeta, bool isNegative)
        {
            this.axiom = (ast.Children[0].Symbol as SymbolTokenText).ValueText;
            this.parameters = new List<PatternPart>();
            this.isMeta = isMeta;
            this.isNegative = isNegative;
            foreach (SyntaxTreeNode param in ast.Children[1].Children)
                this.parameters.Add(TransformationRule.GetPatternPart(context, param));
        }


        public void Write(System.IO.StreamWriter writer)
        {
            if (isMeta) writer.Write("^");
            else writer.Write(" ");
            writer.Write(" ");
            if (isNegative) writer.Write("-");
            else writer.Write(" ");
            writer.Write(" ");

            writer.Write(axiom);
            writer.Write("(");
            bool first = true;
            foreach (PatternPart part in parameters)
            {
                if (!first) writer.Write(" ");
                first = false;
                part.Write(writer);
            }
            writer.Write(")");
        }

        public void WriteOWL(System.IO.StreamWriter writer)
        {
            if (isMeta) writer.Write("Meta(");
            if (isNegative) writer.Write("Not(");

            writer.Write(axiom);
            writer.Write("(");
            bool first = true;
            foreach (PatternPart part in parameters)
            {
                if (!first) writer.Write(" ");
                first = false;
                part.WriteOWL(writer);
            }
            writer.Write(")");

            if (isNegative) writer.Write(")");
            if (isMeta) writer.Write(")");
        }
    }
}
